package com.magic.gre.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.base.fragment.BaseMVPFragment;
import com.magic.gre.entity.StatisticalInfo;
import com.magic.gre.entity.UserBean;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.helper.Contract;
import com.magic.gre.mvp.contract.MineContract;
import com.magic.gre.mvp.presenter.MinePresenterImpl;
import com.magic.gre.ui.activity.LearnSetActivity;
import com.magic.gre.ui.activity.LoginActivity;
import com.magic.gre.ui.activity.ModifyPerActivity;
import com.magic.gre.ui.activity.MyActivityActivity;
import com.magic.gre.ui.activity.MyMsgActivity;
import com.magic.gre.ui.activity.NewWordsActivity;
import com.magic.gre.ui.activity.OffLineActivity;
import com.magic.gre.ui.activity.RipeWordsActivity;
import com.magic.gre.ui.activity.SystemSettingActivity;
import com.magic.gre.ui.activity.WebActivity;
import com.magic.gre.ui.activity.WordCountActvity;
import com.magic.gre.utils.PermissonUtils;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.glide.LoadImageUtils;
import com.noname.lib_base_java.net.RxBus;
import com.noname.lib_base_java.util.PathUtil;
import com.noname.lib_base_java.util.StatusBarUtil;
import com.noname.lib_base_java.util.ToastUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Author: Lmoumou
 * Date: 2019-01-07 15:32
 * 文件名: MineFragment
 * 描述:
 */
public class MineFragment extends BaseMVPFragment<MinePresenterImpl> implements MineContract.View {

    @BindView(R.id.status_bar_v)
    View statusView;
    @BindView(R.id.head_iv)
    ImageView headIv;
    @BindView(R.id.nick_tv)
    TextView nickTv;
    @BindView(R.id.login_tv)
    TextView loginTv;
    @BindView(R.id.words_num_tv)
    TextView wordsNumTv;
    @BindView(R.id.day_tv)
    TextView dayTv;
    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.mNestedScrollView)
    NestedScrollView mNestedScrollView;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    private RxPermissions rxPermissions;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }


    @Override
    protected MinePresenterImpl creatPresenter() {
        return new MinePresenterImpl(this);
    }

    @Override
    protected void lazyLoad() {
        mPresenter.pStatistical();
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void handlerMsg(MsgEvent msgEvent) {
        super.handlerMsg(msgEvent);
        switch (msgEvent.getEvent()) {
            case MsgEvent.UPDATE_PER:
            case MsgEvent.LOGIN_REGISTER:
                updataPer();
                mPresenter.pStatistical();
                break;
        }
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        registerRxBus();
        StatusBarUtil.setHeight(mContext, statusView);
        updataPer();
    }

    private void updataPer() {
        UserBean userInfo = Apphelper.getUserInfo();
        loginTv.setVisibility(Apphelper.isLogin() ? View.GONE : View.VISIBLE);
        nickTv.setVisibility(Apphelper.isLogin() ? View.VISIBLE : View.GONE);
        if (Apphelper.isLogin() && userInfo != null) {
            LoadImageUtils.loadImage(PathUtil.imgPath(userInfo.getPhoto()), headIv);
            nickTv.setText(userInfo.getNickname());
        }
    }

    @OnClick(value = {R.id.right_iv, R.id.custom_tv, R.id.about_tv, R.id.off_line_tv, R.id.activity_tv,
            R.id.learn_set_tv, R.id.ripe_words_tv, R.id.new_words_tv, R.id.msg_tv, R.id.head_iv, R.id.nick_tv,
            R.id.login_tv, R.id.count_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_iv:
                SystemSettingActivity.startThis(mContext);
                break;
            case R.id.custom_tv:
                WebActivity.startThis(mContext, "客服服务", Contract.COUSTOM_URL);
                break;
            case R.id.about_tv:
                WebActivity.startThis(mContext, "关于我们", Contract.ABOUT_URL);
                break;
            case R.id.off_line_tv:
                if (rxPermissions == null) {
                    rxPermissions = new RxPermissions(this);
                }

                rxPermissions.request(PermissonUtils.READ_WRITE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    OffLineActivity.startThis(mContext);
                                } else {
                                    ToastUtil.getInstance().showNormal(mContext, "权限获取失败");
                                }
                            }
                        });


                break;
            case R.id.activity_tv:
                MyActivityActivity.startThis(mContext);
                break;
            case R.id.learn_set_tv:
                LearnSetActivity.startThis(mContext);
                break;
            case R.id.ripe_words_tv:
                RipeWordsActivity.startThis(mContext);
                break;
            case R.id.new_words_tv:
                NewWordsActivity.startThis(mContext);
                break;
            case R.id.msg_tv:
                MyMsgActivity.startThis(mContext);
                break;
            case R.id.head_iv:
            case R.id.nick_tv:
                ModifyPerActivity.startThis(mContext);
                break;
            case R.id.login_tv:
                LoginActivity.startThis(mContext);
                break;
            case R.id.count_tv:
                WordCountActvity.startThis(mContext);
                break;
        }
    }

    @Override
    public void onNoLogin() {
        showLoglinDialog();
    }

    @Override
    public void onCancel() {
        MsgEvent msgEvent = new MsgEvent(MsgEvent.SHOW_MODLE);
        msgEvent.put("tag", 0);
        RxBus.getInstance().post(msgEvent);
    }

    @Override
    public void onDialogMiss() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void vStatiscal(StatisticalInfo statisticalInfo) {
        wordsNumTv.setText(String.valueOf(statisticalInfo.getToday_repeat_count()));
        dayTv.setText(String.valueOf(statisticalInfo.getLearn_days_count() + "天"));
        timeTv.setText(String.valueOf(getTime(statisticalInfo.getLearn_count())));
    }

    private static String getTime(int s) {
        String s1 = "0时0分";
        if (s == 0) {
            return s1;
        }
        String hours = String.valueOf(s / 3600);
        String mini = String.valueOf(s / 60);
        return hours + "时" + mini + "分";
    }
}
